//
//  DYDashboardCell.m
//  PrivatePhoto
//
//  Created by PrivatePhoto Team on 2021.
//  Copyright © 2021年 PrivatePhoto Team. All rights reserved.
//

#import "DYDashboardCell.h"

@implementation DYDashboardCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setData:(id)data delegate:(id)delegate {
    
    if ([data isKindOfClass:[NSDictionary class]]) {
        if ([data[@"data"] isKindOfClass:[BCDashboardCellModel class]]) {
            
            BCDashboardCellModel *model = data[@"data"];
            
            // 移除之前的子控件
            [_innerRingCircleView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];

            //已完成百分比
            float progress = model.speed / 6;
            if ([model.unit isEqualToString:@"KB/s"]) {
                progress = model.speed / (6*1024);
            } else if ([model.unit isEqualToString:@"bytes/s"]) {
                progress = model.speed / (6*1024*1024);
            } else if ([model.unit isEqualToString:@"GB/s"]) {
                progress = 100;
            } else if ([model.unit isEqualToString:@"MB/s"]) {
                if (model.speed >= 6) {
                    progress = 1;
                }
            }
                        
            //内环
            XLCircleProgress *innerRingCircleProgress = [[XLCircleProgress alloc] initWithFrame:_innerRingCircleView.bounds];
            innerRingCircleProgress.color = UIColorFromHex(0x45C4A7);
            innerRingCircleProgress.progress = progress;
            [_innerRingCircleView addSubview:innerRingCircleProgress];
            
            if (model.speed > 0) {
                _speedLabel.text = [NSString stringWithFormat:@"%.2f",model.speed];
                _unitLabel.text = model.unit;
            } else {
                _speedLabel.text = @"0";
                _unitLabel.text = @"KB/s";
            }
            
        }
    } 
}

@end


#pragma mark - Model

@implementation BCDashboardCellModel

+ (BCDashboardCellModel *)ittemModelWithSpeed:(float)speed unit:(NSString *)unit {
    BCDashboardCellModel *model = [BCDashboardCellModel new];
    model.speed = speed;
    model.unit = kSafeString(unit);
    return model;
}

@end
